package personal.xjl._23java2studentdemo.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import personal.xjl._23java2studentdemo.mapper.StudentMapper;
import personal.xjl._23java2studentdemo.model.Student;
import personal.xjl._23java2studentdemo.service.StudentService;

import java.util.List;

@Service //相当于@Controleer,@Mapper,告诉spring容器帮我创建一个service的对象// 标识这是一个Spring服务组件
public class StudentServiceImpl implements StudentService {

    @Autowired// 自动注入学生Mapper
    StudentMapper studentMapper;

    @Override
    public List<Student> findAll() {
        return studentMapper.selectAll();
    }

    @Override
    public int remove(int id) {
        return studentMapper.deleteById(id);
    }

    @Override
    public int add(Student student) {
        return studentMapper.insert(student);
    }

    @Override
    public int edit(Student student) {
        return studentMapper.update(student);
    }

    @Override
    public Student findOneById(int id) {
        return studentMapper.selectById(id);
    }

    @Override
    public List<Student> findByPage(int pageNo, int pageSize) {
        //如果页号<=0,则从第一条记录开始，以防异常
        int fromIndex =(pageNo<1)?0:(pageNo - 1) * pageSize;
        return studentMapper.selectByPage(fromIndex, pageSize);
    }

    @Override
    public boolean login(String username, String password) {
        //如果从数据库student表中能查到一条记录，则返回true,否则返回false
        return studentMapper.selectByNameAndPwd(username,password)!=null;
    }
}
